package com.shanling.shanlingcontroller.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.Size;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qualcomm.qti.libraries.gaia.GAIA;
import com.qualcomm.qti.libraries.vmupgrade.UpgradeError;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.bean.MyBleDeviceListBean;
import com.shanling.shanlingcontroller.bean.MybluetoothDevice;
import com.shanling.shanlingcontroller.ui.activity.BleDevicecontrolActivity;
import com.shanling.shanlingcontroller.ui.activity.EQActivity;
import com.shanling.shanlingcontroller.ui.activity.OTAActivity;
import com.shanling.shanlingcontroller.utils.EQBeansUtils;
import com.shanling.shanlingcontroller.utils.PreferenceUtil;
import com.shanling.shanlingcontroller.utils.ToastUtils;
import com.shanling.shanlingcontroller.view.SwipeMenuLayout;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ch1 on 2019/11/28.
 */

public class BleDeviceListAdapter  extends SectionedRecyclerViewAdapter<BleDeviceListAdapter.HeaderHolder,
        BleDeviceListAdapter.ContentHolder,RecyclerView.ViewHolder>  {
    private List<MyBleDeviceListBean> listData;
    private Context mContext;
    private LayoutInflater mInflater;

    public BleDeviceListAdapter(List<MyBleDeviceListBean> listData, Context mContext) {
        this.listData = listData;
        this.mContext = mContext;
        mInflater=LayoutInflater.from(mContext);
    }


    @Override
    protected int getSectionCount() {
        if(listData==null||listData.size()==0){
            return 0;
        }
        return listData.size();
    }

    @Override
    protected int getItemCountForSection(int section) {
        List<MybluetoothDevice> infos = listData.get(section).getMybluetoothDevices();
        if(infos==null||infos.size()==0){
            return 0;
        }
        return infos.size();
    }

    @Override
    protected boolean hasFooterInSection(int section) {
        return false;
    }

    @Override
    protected HeaderHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
        return new HeaderHolder(mInflater.inflate(R.layout.item_header,parent,false));
    }

    @Override
    protected RecyclerView.ViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    protected ContentHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        return new ContentHolder(mInflater.inflate(R.layout.item_content,parent,false));
    }

    @Override
    protected void onBindSectionHeaderViewHolder(HeaderHolder holder, int section) {
        if(listData==null||listData.size()==0){
            return;
        }
        String tagsName = listData.get(section).getTagName();
        holder.tv_herader.setText(tagsName);
    }
    @Override
    protected void onBindSectionFooterViewHolder(RecyclerView.ViewHolder holder, int section) {

    }

    @Override
    protected void onBindItemViewHolder(ContentHolder holder, int section, int position, int original_postion) {
        if(listData==null||listData.size()==0){
            return;
        }
        List<MybluetoothDevice> infos = listData.get(section).getMybluetoothDevices();
        if(infos==null||infos.size()==0){
            return ;
        }
        String tagsName = listData.get(section).getTagName();
        MybluetoothDevice mybluetoothDevice = infos.get(position);
        holder.tv_name.setText(mybluetoothDevice.getName());
         holder.tv_currentversion.setText(mContext.getString(R.string.current_version)+mybluetoothDevice.getCurrent_Version());
        if(tagsName.equals(mContext.getString(R.string.connected))){
            holder.rl_content.setBackground(mContext.getDrawable(R.drawable.item_check));
            holder.iv_update.setVisibility(View.VISIBLE);
            holder.swipeMenuLayout.setSwipeEnable(false);
        }else {
            holder.rl_content.setBackground(mContext.getDrawable(R.drawable.item_nor));
            holder.iv_update.setVisibility(View.GONE);
            if(mybluetoothDevice.getMac().equals(PreferenceUtil.getInstance(mContext).getBleMac())){
                holder.swipeMenuLayout.setSwipeEnable(false);
            }else {
                holder.swipeMenuLayout.setSwipeEnable(true);
            }
        }
        holder.iv_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(OTAActivity.class);
            }
        });
        holder.tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LitePal.deleteAll(MybluetoothDevice.class,"mac like ?",infos.get(position).getMac());
                infos.remove(position);
                notifyDataSetChanged();
                holder.swipeMenuLayout.quickClose();
            }
        });
        holder.rl_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(tagsName.equals(mContext.getString(R.string.connected))){
                    switch (mybluetoothDevice.getType()){
                        case 1:
                        case 2:
                            startActivity(EQActivity.class);
                            break;
                        case 3:
                            startActivity(BleDevicecontrolActivity.class);
                            break;
                    }
                }
            }
        });
    }
    private void startActivity(Class clazz){
        mContext.startActivity(new Intent(mContext,clazz));
    }
    static class HeaderHolder extends RecyclerView.ViewHolder {
        private TextView tv_herader;
        public HeaderHolder(View itemView) {
            super(itemView);
            tv_herader=itemView.findViewById(R.id.tv_herader);
        }
    }
    static class  ContentHolder extends RecyclerView.ViewHolder{
        private SwipeMenuLayout swipeMenuLayout;
        private RelativeLayout rl_content;
        private TextView tv_name;
        private ImageView iv_update;
        private TextView tv_currentversion;
        private TextView tv_delete;
        public ContentHolder( View itemView) {
            super(itemView);
            swipeMenuLayout=itemView.findViewById(R.id.sw);
            tv_name =itemView.findViewById(R.id.tv_name);
            iv_update=itemView.findViewById(R.id.iv_update);
            rl_content=itemView.findViewById(R.id.rl_content);
            tv_currentversion=itemView.findViewById(R.id.tv_currentversion);
            tv_delete=itemView.findViewById(R.id.tv_delete);
        }
    }
}
